package elearning.controller.admin.regist;

import java.util.ArrayList;
import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Key;

import elearning.model.Option;
import elearning.model.Question;
import elearning.service.ElearningService;

public class EditQuestionController extends Controller {

	private static final int OPTION_COUNT_MAX = 6;

	private ElearningService service = new ElearningService();

	@Override
	public Navigation run() {
		Key key = asKey("key");
		Long version = asLong("version");
		Question question = service.getQuestion(key, version);
		requestScope("question", question.getQuestion());
		List<Option> list = service.getOptionByQuestion(question.getKey());
		for (int i = 0; i < list.size(); i++) {
			requestScope("option" + (i + 1), list.get(i).getOption());
		}
		for (int i = list.size(); i < OPTION_COUNT_MAX; i++) {
			requestScope("option" + (i + 1), "");
		}
		List<String> correctList = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			Option option = list.get(i);
			if (option.getIsCorrect()) {
				correctList.add(String.valueOf(i + 1));
			}
		}
		requestScope("correctArray", correctList.toArray(new String[correctList
				.size()]));
		return forward("editQuestion.jsp");
	}
}
